<template>
  <div class="comment">
    <hmheader title="精彩评论页">
      <template #left>
        <van-icon name="arrow-left" @click="$router.go(-1)" />
      </template>
    </hmheader>
    <div class="list">
      <div class="item" v-for="value in commentList" :key="value.id">
        <div class="head">
          <img :src="axios.defaults.baseURL + value.user.head_img" alt />
          <div>
            <p>{{ value.user.nickname }}</p>
            <span>时间</span>
          </div>
          <span @click="replayComment(value)">回复</span>
        </div>
        <!-- 嵌套子组件 -->
        <hmcommentitem
          v-if="value.parent"
          :parent="value.parent"
          @replay="replayComment"
        ></hmcommentitem>

        <div class="text">{{ value.content }}</div>
      </div>
    </div>
    <!-- 底部评论 -->
    <hmCommentFooter
      :post="article"
      @refresh="refresh"
      :commentObj="temp"
      @cancel="temp = null"
    ></hmCommentFooter>
  </div>
</template>

<script>
import hmheader from "@/components/hmheader.vue";
import hmCommentFooter from "@/components/hmCommentFooter.vue";
import hmcommentitem from "@/components/hmcommentitem.vue";
import { getPostComment, getPostDetail } from "@/apis/post.js";
import axios from "@/utils/myaxios.js";
export default {
  components: {
    hmheader,
    hmcommentitem,
    hmCommentFooter,
  },
  data() {
    return {
      commentList: [],
      axios,
      article: {},
      temp: {},
    };
  },
  async mounted() {
    // let id = this.$route.params.id;
    // // 获取文章详情数据
    // article = (await getPostDetail(id)).data.data;
    // let res = await getPostComment(id);
    // console.log(res);
    // this.commentList = res.data.data;
    this.init();
  },
  methods: {
    //   回复子组件的评论
    replayComment(v) {
      console.log("父组件中的", v);
      this.temp = v;
    },
    async init() {
      let id = this.$route.params.id;
      // 获取文章详情数据
      this.article = (await getPostDetail(id)).data.data;
      //   获取文章的评论数据
      let res = await getPostComment(id);
      console.log(res);
      this.commentList = res.data.data;
    },
    refresh() {
      this.init();
      // 让列表自动的滚到顶部
      window.scrollTo(0, 0);
    },
  },
};
</script>

<style lang="less" scoped>
.list {
  padding-bottom: 60px;
}
.item {
  padding: 10px;
  border-bottom: 1px solid #ccc;
  .head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    > img {
      width: 50/360 * 100vw;
      height: 50/360 * 100vw;
      display: block;
      border-radius: 50%;
    }
    > div {
      flex: 1;
      display: flex;
      flex-direction: column;
      margin-left: 10px;
      > span {
        font-size: 12px;
        color: #999;
        line-height: 25px;
      }
    }
    > span {
      color: #999;
      font-size: 13px;
    }
  }
  .text {
    font-size: 14px;
    color: #333;
    padding: 20px 0 10px 0;
  }
}
</style>